﻿using System;
using System.Collections.Generic;
using System.Data;
using libexcel;

namespace libwutong
{
    class MyRefer
    {   
        static public DataTable getTableRefer(string folder, string unkown_ids_txt_include_path, bool isPrintf)
        {
            int iIdForReal = -1;
            int iWeight = -1;
            int iScanType = -1;
            int iScanNode = -1;
            int iScanTime = -1;
            int iScanStaff = -1;
            int iReceiveStaff = -1;



            int iIdForArea = -1;
            int iSign = -1;
            int iSignTemp;
            bool isFound;
            string str;
            DataTable tableReal = new DataTable();
            DataTable tableArea = new DataTable();
            DataTable tableWeight = new DataTable();

            TxtWeight txtWeight = new TxtWeight();
            List<string> ids = new List<string>();
            string[] files = System.IO.Directory.GetFiles(folder);
            foreach (string file in files)
            {
                DataTable table = ExcelHelper.getTable(file);
                // 是否有"签收网点"字段
                iSignTemp = table.Columns.IndexOf(MyTxt.SignField);
                if (iSignTemp < 0)
                {
                    if (tableReal.Columns.Count == 0)
                    {
                        iIdForReal = table.Columns.IndexOf(MyTxt.IdField);
                        iWeight = table.Columns.IndexOf(MyTxt.WeightField);
                        iScanType = table.Columns.IndexOf(MyTxt.ScantTypeField);
                        iScanNode = table.Columns.IndexOf(MyTxt.ScanNodeField);
                        iScanTime = table.Columns.IndexOf(MyTxt.ScanTimeField);
                        iScanStaff = table.Columns.IndexOf(MyTxt.ScanStaffField);
                        iReceiveStaff = table.Columns.IndexOf(MyTxt.ReceiveStaffField);

                        for (int i = 0; i < table.Columns.Count; ++i)
                        {
                            tableReal.Columns.Add(table.Columns[i].ToString());
                        }
                        tableWeight.Columns.Add(MyTxt.IdField);
                        tableWeight.Columns.Add(MyTxt.WeightField);
                    }

                    for (int k = 0; k < table.Rows.Count; ++k)
                    {
                        str = table.Rows[k][iIdForReal].ToString();
                        if(ids.IndexOf(str) < 0)
                        {
                            ids.Add(str);
                        }
                        DataRow row = table.Rows[k];
                        if(row[iScanType].ToString() == txtWeight.ScantTypeReceive &&
                            row[iScanNode].ToString() == txtWeight.ScanNodeReceive)
                        {
                            isFound = false;
                            for (int k2 = 0; k2 < tableReal.Rows.Count; ++k2)
                            {
                                if(tableReal.Rows[k2][iIdForReal].ToString() == str)
                                {
                                    isFound = true;
                                    break;
                                }
                            }
                            if(!isFound)
                            {
                                row = tableReal.NewRow();
                                for (int i = 0; i < table.Columns.Count; ++i)
                                {
                                    row[i] = table.Rows[k][i];
                                }
                                tableReal.Rows.Add(row);
                            }

                        }
                        else if (row[iScanType].ToString() == txtWeight.ScantTypeArrive &&
                            row[iScanNode].ToString() == txtWeight.ScanNodeArrive)
                        {
                            isFound = false;
                            for (int k2 = 0; k2 < tableWeight.Rows.Count; ++k2)
                            {
                                if (tableWeight.Rows[k2][0].ToString() == str)
                                {
                                    isFound = true;
                                    break;
                                }
                            }
                            if(!isFound)
                            {
                                row = tableWeight.NewRow();
                                row[0] = table.Rows[k][iIdForReal];
                                row[1] = table.Rows[k][iWeight];
                                tableWeight.Rows.Add(row);
                            }
                        }
                    }
                }
                else
                {
                    if (tableArea.Columns.Count == 0)
                    {
                        iIdForArea = table.Columns.IndexOf(MyTxt.IdField);
                        iSign = iSignTemp;
                        tableArea.Columns.Add(MyTxt.IdField);
                        tableArea.Columns.Add(MyTxt.SignField);
                    }
                    for (int k = 0; k < table.Rows.Count; ++k)
                    {
                        str = table.Rows[k][iIdForArea].ToString();
                        if (ids.IndexOf(str) < 0)
                        {
                            ids.Add(str);
                        }
                        isFound = false;
                        for (int k2 = 0; k2 < tableArea.Rows.Count; ++k2)
                        {
                            if (tableArea.Rows[k2][0].ToString() == str)
                            {
                                isFound = true;
                                break;
                            }
                        }
                        if(!isFound)
                        {
                            // 运单编号, 签收网点
                            DataRow row = tableArea.NewRow();
                            row[0] = table.Rows[k][iIdForArea];
                            row[1] = table.Rows[k][iSign];
                            tableArea.Rows.Add(row);
                        }
                    }
                }
                if (isPrintf)
                {
                    Console.Write(".");
                }
            }


            iSignTemp = ids.Count;
            for (int k = 0; k < ids.Count; ++k)
            {
                if (k < tableReal.Rows.Count &&
                    k < tableArea.Rows.Count &&
                    k < tableWeight.Rows.Count &&
                    tableReal.Rows[k][iIdForReal].ToString() == tableArea.Rows[k][0].ToString() &&
                    tableReal.Rows[k][iIdForReal].ToString() == tableWeight.Rows[k][iIdForReal].ToString())
                {

                }
                else
                {
                    iSignTemp = k;
                    break;
                }
            }

            DataTable tableRefer = new DataTable();
            tableRefer.Columns.Add(MyTxt.IdField);
            tableRefer.Columns.Add(MyTxt.ReceiveStaffField);
            tableRefer.Columns.Add(MyTxt.SignField);
            tableRefer.Columns.Add(MyTxt.WeightField + "(" + txtWeight.ScantTypeReceive + ")");
            tableRefer.Columns.Add(MyTxt.WeightField + "(" + txtWeight.ScanNodeArrive + ")");
            tableRefer.Columns.Add(MyTxt.ScanStaffField);
            tableRefer.Columns.Add(MyTxt.ScanTimeField);

            
            for (int k = 0; k < iSignTemp; ++k)
            {
                DataRow row = tableRefer.NewRow();
                row[0] = tableReal.Rows[k][iIdForReal];
                row[1] = tableReal.Rows[k][iReceiveStaff];
                row[2] = tableReal.Rows[k][iSign];
                row[3] = tableReal.Rows[k][iWeight];
                row[4] = tableWeight.Rows[k][1];
                row[5] = tableReal.Rows[k][iScanStaff];
                row[6] = tableReal.Rows[k][iScanTime];
                tableRefer.Rows.Add(row);
            }

            List<string> id2s = new List<string>();
            List<string> id3s = new List<string>();
            int iReal, iWei, iArea;
            for (int k = iSignTemp; k < ids.Count; ++k)
            {
                iReal = iWei = iArea = -1;
                for(int k2 = iSignTemp; k2 < tableReal.Rows.Count; ++k2)
                {
                    str = tableReal.Rows[k2][iIdForReal].ToString();
                    if(str == ids[k])
                    {
                        iReal = k2;
                        break;
                    }
                }

                for (int k2 = iSignTemp; k2 < tableWeight.Rows.Count; ++k2)
                {
                    str = tableWeight.Rows[k2][0].ToString();
                    if (str == ids[k])
                    {
                        iWei = k2;
                        break;
                    }
                }


                for (int k2 = iSignTemp; k2 < tableArea.Rows.Count; ++k2)
                {
                    str = tableArea.Rows[k2][0].ToString();
                    if (str == ids[k])
                    {
                        iArea = k2;
                        break;
                    }
                }

                if(iReal < 0 || iArea < 0)
                {
                    if (ids[k].Length == MyExcel.s_idLen)
                    {
                        id2s.Add(ids[k]);
                    }
                    else
                    {
                        id3s.Add(ids[k]);
                    }
                    continue;
                }

                DataRow row = tableRefer.NewRow();

                row[0] = tableReal.Rows[iReal][iIdForReal];
                row[1] = tableReal.Rows[iReal][iReceiveStaff];
                row[2] = tableArea.Rows[iArea][1];
                row[3] = tableReal.Rows[iReal][iWeight];
                row[4] = "0";
                if(iWei >= 0)
                {
                    row[4] = tableWeight.Rows[iWei][1];
                }
                row[5] = tableReal.Rows[iReal][iScanStaff];
                row[6] = tableReal.Rows[iReal][iScanTime];

                tableRefer.Rows.Add(row);
            }

            iSignTemp = id2s.Count + id3s.Count;
            if (iSignTemp > 0)
            {
                if (isPrintf)
                {
                    Console.WriteLine(" 违规或不明编号{0}个", iSignTemp);
                    Console.WriteLine(" 已导出 {0},请自行处理", unkown_ids_txt_include_path);
                }
                if(System.IO.File.Exists(unkown_ids_txt_include_path))
                {
                    System.IO.File.Delete(unkown_ids_txt_include_path);
                }
                System.IO.FileStream fs = new System.IO.FileStream(unkown_ids_txt_include_path, System.IO.FileMode.Create);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);

                //开始写入
                sw.Write("{0}个编号请自行处理\r\n", iSignTemp);
                foreach(string id in id2s)
                {
                    sw.Write("{0}\r\n", id);
                }
                foreach (string id in id3s)
                {
                    sw.Write("{0}\r\n", id);
                }

                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }

            return tableRefer;
        }

    }
}
